<?php
/**
 * Save.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/14
 * Time: 13:20
 */
namespace app\api\open\member\order;

use app\api\open\member\Token;
use app\api\model\OrderModel;
use think\Db;
use think\Validate;

class Save extends Token
{
    protected $id = null;
    protected $saveData = [];
    /**
     * Save constructor.
     * @throws \Exception
     */
    public function __construct($param=[])
    {
        parent::__construct($param);

        // 验证请求参数
        $validate = new Validate([
			"store_id" => "require",
			"receive_address_id" => "require",
			"receive_province" => "require",
			"receive_city" => "require",
			"receive_county" => "require",
			"express_id" => "require",
			"payment_id" => "require",
			"pay_time" => "require",
			"confirm_time" => "require",
			"delivery_time" => "require",
			"cancel_time" => "require",
			"collect_time" => "require",
			"close_time" => "require",
			"is_comments" => "require",
			"member_is_comments" => "require",
			"is_delete_time" => "require",
			"member_is_delete_time" => "require",
			"increase_price" => "require",
			"preferential_price" => "require",
			"price" => "require",
			"total_price" => "require",
			"pay_price" => "require",
			"receive_tel" => "require",
			"order_no" => "require",
			"receive_name" => "require",
			"receive_address" => "require",
			"member_note" => "require",
			"express_number" => "require",
			"extension_data" => "require",
            "goods_list" => "require"
        ],[
			"store_id.require" => "商店id必填",
			"receive_address_id.require" => "收件地址id必填",
			"receive_province.require" => "收件人-省必填",
			"receive_city.require" => "收件人-市必填",
			"receive_county.require" => "收件人-县/区必填",
			"express_id.require" => "快递id必填",
			"payment_id.require" => "支付方式id必填",
			"pay_time.require" => "支付时间必填",
			"confirm_time.require" => "确认时间必填",
			"delivery_time.require" => "发货时间必填",
			"cancel_time.require" => "取消时间必填",
			"collect_time.require" => "收货时间必填",
			"close_time.require" => "关闭时间必填",
			"is_comments.require" => "商家是否已评论（0否, 大于0评论时间）必填",
			"member_is_comments.require" => "用户是否已评论（0否, 大于0评论时间）必填",
			"is_delete_time.require" => "商家是否已删除（0否, 大于0删除时间）必填",
			"member_is_delete_time.require" => "用户是否已删除（0否, 大于0删除时间）必填",
			"increase_price.require" => "增加的金额必填",
			"preferential_price.require" => "优惠金额必填",
			"price.require" => "订单单价必填",
			"total_price.require" => "订单总价(订单最终价格)必填",
			"pay_price.require" => "已支付金额必填",
			"receive_tel.require" => "收件人-电话必填",
			"order_no.require" => "订单号必填",
			"receive_name.require" => "收件人-姓名必填",
			"receive_address.require" => "收件人-详细地址必填",
			"member_note.require" => "用户备注必填",
			"express_number.require" => "快递单号必填",
			"extension_data.require" => "扩展展示数据必填",
            "goods_list.require" => "下单商品必填"
        ]);
        $check = $validate->check($this->params);
        if($check === false){
            // 验证失败 输出错误信息
            throw new \Exception($validate->getError(),1);
        }
        if(!empty($this->params['id']))
        {
            $this->id = $this->params['id'];
        }
        /**
         * 根据商店ID分组拆单
         */
        $goods_list = json_decode($this->params['goods_list'],true);
        foreach($goods_list as $goods)
        {
            if(empty($this->saveData[$goods['store_id']]))
            {
                $this->saveData[$goods['store_id']] = [];
            }
            $this->saveData[$goods['store_id']][] = [

            ];
        }
		// 订单状态（0待确认, 1已确认/待支付, 2已支付/待发货, 3已发货/待收货, 4已完成, 5已取消, 6已关闭）
		$this->saveData['status'] = 1;
		// 支付状态（0未支付, 1已支付, 2已退款）
		$this->saveData['pay_status'] = 0;
		// 会员ID
		$this->saveData['member_id'] = $this->member_id;
		// 商店id
		$this->saveData['store_id'] = (int)$this->params['store_id'];
		// 收件地址id
		$this->saveData['receive_address_id'] = (int)$this->params['receive_address_id'];
		// 收件人-省
		$this->saveData['receive_province'] = (int)$this->params['receive_province'];
		// 收件人-市
		$this->saveData['receive_city'] = (int)$this->params['receive_city'];
		// 收件人-县/区
		$this->saveData['receive_county'] = (int)$this->params['receive_county'];
		// 快递id
		$this->saveData['express_id'] = (int)$this->params['express_id'];
		// 支付方式id
		$this->saveData['payment_id'] = (int)$this->params['payment_id'];
		// 支付时间
		$this->saveData['pay_time'] = (int)$this->params['pay_time'];
		// 确认时间
		$this->saveData['confirm_time'] = (int)$this->params['confirm_time'];
		// 发货时间
		$this->saveData['delivery_time'] = (int)$this->params['delivery_time'];
		// 取消时间
		$this->saveData['cancel_time'] = (int)$this->params['cancel_time'];
		// 收货时间
		$this->saveData['collect_time'] = (int)$this->params['collect_time'];
		// 关闭时间
		$this->saveData['close_time'] = (int)$this->params['close_time'];
		// 商家是否已评论（0否, 大于0评论时间）
		$this->saveData['is_comments'] = (int)$this->params['is_comments'];
		// 用户是否已评论（0否, 大于0评论时间）
		$this->saveData['member_is_comments'] = (int)$this->params['member_is_comments'];
		// 商家是否已删除（0否, 大于0删除时间）
		$this->saveData['is_delete_time'] = (int)$this->params['is_delete_time'];
		// 用户是否已删除（0否, 大于0删除时间）
		$this->saveData['member_is_delete_time'] = (int)$this->params['member_is_delete_time'];
		// 增加的金额
		$this->saveData['increase_price'] = $this->params['increase_price'];
		// 优惠金额
		$this->saveData['preferential_price'] = $this->params['preferential_price'];
		// 订单单价
		$this->saveData['price'] = $this->params['price'];
		// 订单总价(订单最终价格)
		$this->saveData['total_price'] = $this->params['total_price'];
		// 已支付金额
		$this->saveData['pay_price'] = $this->params['pay_price'];
		// 收件人-电话
		$this->saveData['receive_tel'] = (string)$this->params['receive_tel'];
		// 订单号
		$this->saveData['order_no'] = (string)$this->params['order_no'];
		// 收件人-姓名
		$this->saveData['receive_name'] = (string)$this->params['receive_name'];
		// 收件人-详细地址
		$this->saveData['receive_address'] = (string)$this->params['receive_address'];
		// 用户备注
		$this->saveData['member_note'] = (string)$this->params['member_note'];
		// 快递单号
		$this->saveData['express_number'] = (string)$this->params['express_number'];
		// 扩展展示数据
		$this->saveData['extension_data'] = (string)$this->params['extension_data'];
    }
    
    /**
     * @param array $param
     * @return array
     * @throws \Exception
     */
    public function index($param=[])
    {
        Db::startTrans();
        try
        {
            // 保存信息
            $id = OrderModel::getInstance()->save($this->id,$this->saveData);
            if(!$id)
            {
                throw new \Exception("保存失败",2);
            }
        }
        catch (\Exception $e)
        {
            // 出现错误进行回滚处理
            Db::rollback();
            // 统一错误抛出
            throw new \Exception("保存失败：".$e->getMessage(),$e->getCode());
        }

        Db::commit();
        return ['id'=>empty($this->id)?$id:$this->id];
    }
}